package com.mybatisPlusSpring;

import com.malred.mappers.orderMapper;
import com.malred.mappers.personMapper;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;
import java.util.Map;

/**
 * @author malguy-wang sir
 * @create ---
 */
public class myImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(myScan.class.getName());
        String path = (String) annotationAttributes.get("value");
        System.out.println(path);
        myScanner scanner = new myScanner(registry);
        scanner.addIncludeFilter(new TypeFilter() {
            @Override
            public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
                return true;
            }
        });
        scanner.scan(path);
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
        beanDefinition.setBeanClass(myFactoryBean.class);
        //手动指定mapper
        beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(personMapper.class);
        registry.registerBeanDefinition("personMapper",beanDefinition);

        AbstractBeanDefinition beanDefinition1 = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
        beanDefinition1.setBeanClass(myFactoryBean.class);
        //手动指定mapper
        beanDefinition1.getConstructorArgumentValues().addGenericArgumentValue(orderMapper.class);
        registry.registerBeanDefinition("orderMapper",beanDefinition1);
    }
}
